//
//  KSKACollectionView.m
//  DashboardHD
//
//  Created by 楊亜玲 on 17/4/1.
//  Copyright © 2017年 Totyu. All rights reserved.
//  

#import "KSKACategoryView.h"
#import "KSKACategoryTool.h"
#import "KSKACategory.h"

@interface KSKACategoryCell()
@property (weak, nonatomic) IBOutlet UIButton *categoryColorBtn;
@property (weak, nonatomic) IBOutlet UILabel *categoryLabel;

- (IBAction)colorBtnPressed:(id)sender;

@end

@implementation KSKACategoryCell

-(void)setCategory:(KSKACategory *)category{
    _category = category;
    
    // 1.活动名称
    self.categoryLabel.text = category.actionname;
    // 2.活动颜色
    self.categoryColorBtn.backgroundColor = [UIColor colorWithHex:category.actioncolor];
    
}

- (IBAction)colorBtnPressed:(id)sender {
    
}

@end




@interface KSKACategoryView ()<UICollectionViewDataSource,UICollectionViewDelegate>

/** Category 模型数组 */
@property(nonatomic, strong) NSMutableArray *categoryArray;


@end

@implementation KSKACategoryView

static NSString * const reuseIdentifier = @"categoryCell";


//用xib实例化对象方法会自动调用initWithCoder：aDecoder方法来初始化对象
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        self.delegate = self;
        self.dataSource = self;
        [self loadCategoryData];
    }
    return self;
}

/**
 加载并显示分类信息
 */
-(void)loadCategoryData{
    KSKACategoryParam *param = [[KSKACategoryParam alloc]init];
    param.buildingid = @"2";
    param.floorno = @"3";
    NSMutableArray *categoryArray = [NSMutableArray array];
    [KSKACategoryTool kskSettingWithParam:param success:^(NSArray *array) {
        if (array.count > 0) {
            for (NSDictionary *tmpDict in array) {
                // 加载当前房间的关于“活动”的信息
                if ([[tmpDict valueForKey:@"roomid"] isEqualToString:@"2"]) {
                    NSArray *tmpArr = [tmpDict objectForKey:@"actioninfo"];
                    for (NSDictionary *tmpDict2 in tmpArr) {
                        if ([[tmpDict2 valueForKey:@"actionclass"] isEqualToString:@"2"]) {
                            [categoryArray addObject:tmpDict2];
                            
                        }
                    }
                }
            }
            // 字典数组－>模型数组
            NSMutableArray *arr = [KSKACategory mj_objectArrayWithKeyValuesArray:categoryArray];
            self.categoryArray = arr;
            [self reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.categoryArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    // 1.创建cell
    KSKACategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    // 2.设置字体
    cell.categoryLabel.font = [UIFont systemFontOfSize:16];
    if (self.selectedIndex == indexPath && self.hasSelectedItem == YES) {
        cell.categoryLabel.font = [UIFont boldSystemFontOfSize:16];
    }
    // 3.传递数据模型
    KSKACategory *category = [self.categoryArray objectAtIndex:indexPath.item];
    cell.category = category;
    

    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    KSKACategory *selectedCategory = [self.categoryArray objectAtIndex:indexPath.item];
    NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
    // 1.判断是否有选中的类别
    //   如果有，标记“有选中类别”为NO
    if (self.selectedIndex == indexPath) {
        self.hasSelectedItem = NO;
        self.selectedIndex = nil;
    }else{
        // 如果没有，记录选中类别的index，并且标记“有选中类别”为YES
        self.hasSelectedItem = YES;
        self.selectedIndex = indexPath;
        [dataDict setObject:selectedCategory.actionid forKey:@"actionid"];
    }
    
    // 2.刷新collectionView(改变选中文字字体)
    [self reloadData];
    
    // 3.发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"categorySelected" object:self userInfo:dataDict];
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.width,self.height / _categoryArray.count);
}




@end
